<?php
/**
 * Description of User_OrderController
 * implement 5 action
 * @author: Beverley
 */

class OrderController extends Zend_Controller_Action {
    // view order
    function indexAction() {
        $cate = new Category();
        $this->view->cateList = $cate->fetchAll();

        $productSession = new Zend_Session_Namespace('product');
        if (isset($productSession->product))
        {
            $array = $productSession->product;
            $product = new Product();

            foreach($array as $id=>$quantity)
            {
                if ($quantity > 0) {
                    $row = $product->fetchRow("id = '$id'");

                    if (isset($data)) {
                        $data[$id] = $row;
                        $storeQuantity[$id] = $quantity;
                    } else {
                        $data = Array($id => $row);
                        $storeQuantity = Array($id => $quantity);
                    }
                }
            }

            if (isset($data) && isset($storeQuantity)) {
                $this->view->product = $data;
                $this->view->quantity = $storeQuantity;
            }
        }
    }

    // add product into a cart
    function addproductAction() {
        $cate = new Category();
        $this->view->cateList = $cate->fetchAll();

        $quantity = $this->getRequest()->getPost('quantity');
        if (!isset($quantity)) {
            $quantity = 1;
        }
        $id = $this->getRequest()->getParam('id');

        $productSession = new Zend_Session_Namespace('product');
        if (isset($productSession->product)) {
            $array = $productSession->product;
            $array[$id] = $quantity;
        }
        else {
            $array = Array($id => $quantity);
        }

        $productSession->product = $array;

//        echo $id."<br>";
       $this->_redirect('/order');
    }

    // update information of products in cart
    function updateAction() {
        $cate = new Category();
        $this->view->cateList = $cate->fetchAll();

        $orderDetail = new OrderDetailDAO();
        $productSession = new Zend_Session_Namespace('product');
        $productSession->mess = ""; // use for display input failure

        if (isset($productSession->product) && !$orderDetail->checkCartIsEmpty($productSession->product)) {
            $array = $productSession->product;

            $product = new Product();
            foreach($array as $id=>$quantity)
            {
                // for retrevie number of quantity of the product
                $row = $product->fetchRow("id = '$id'");
                $proQuantity = $row->quantity;
                
                $newquantity = $this->getRequest()->getPost('text'.$id);
                
                $validator = new Zend_Validate_Int();
                // check quantity input is a integer
                if ($validator->isValid($newquantity)) {
                    $validator = new Zend_Validate_Between(0, $proQuantity);
                    // check quantity input is appropriate
                    if ($validator->isValid($newquantity)) {
                        $array[$id] = $newquantity;
                    } else {
                        if ($newquantity > $proQuantity)
                            $productSession->mess = "Quantity input is too large.";
                        if ($newquantity < 0)
                            $productSession->mess = "Invalid quatity input.";
                    }

                } else {
                    $productSession->mess = "Invalid quatity input.";
                }
                $check = $this->getRequest()->getPost($id);
                if ($check == 1) {
                    $array[$id] = 0;
                    $productSession->mess = "";
                }
            }
            $productSession->product = $array;
            $this->_redirect('/order');
        }
        else {
            $this->view->status = "Your cart is empty.";
        }
    }

}
?>
